-- Lua 脚本：预占库存减N，占用库存加N
-- 参数: KEYS[1] 为库存键名前缀和 skuId 组成的完整库存键
-- ARGV[1] 为从预占转为占用的数量

-- 获取当前库存信息
local withholdingQuantity = tonumber(redis.call('HGET', KEYS[1],"withholdingQuantity"))
if not withholdingQuantity then
    error("withholdingQuantity is not a valid number")
end
local occupiedQuantity = tonumber(redis.call('HGET', KEYS[1],"occupiedQuantity"))
if not occupiedQuantity then
    error("occupiedQuantity is not a valid number ")
end

-- 检查预占库存是否足够
if withholdingQuantity < tonumber(ARGV[1]) then
    return false
end

-- 更新预占库存与占用库存
redis.call('HINCRBY', KEYS[1], 'occupiedQuantity', tonumber(ARGV[1]))
redis.call('HINCRBY', KEYS[1], 'withholdingQuantity',-tonumber(ARGV[1]))

return true